<?php
/**
**2015-08-27 对接驴妈妈系统
**User By  PZP
**/
include('/var/www/html/new/d/module/api/QD_underwater_world/QD.class.php');
include('/var/www/html/new/d/module/api/function.class.php');

if (!class_exists("go_sql")){
	include("/var/www/html/new/conf/le.je");
	$le=new go_sql();
	$le->connect();
}

$txt=json_encode($_REQUEST);

$QD = new QD_underW_World();
$PFT_infunction = new AllFunction();

$QD->logfile($txt,'QD_get_data');

//{"VenueID":"90FA3B1508DB4CD3B4D6D70BBEED5BDA","RoundID":"","AreaID":"","SeatID":"","Tnum":"1","Fid":"113","PayStatus":"1","TicketID":"21247","TouristName":"\u5f6d\u5fd7\u840d","TouristTel":"15959110796","PlayDate":"2015-08-26","IdCard":"350321199108230122"}

if(!isset($_REQUEST['Action'])){
	$timestart = microtime(true);
	if(!$_REQUEST['TicketID']) exit("101|0|0|0|0|0");
	
	$tid = $_REQUEST['TicketID'] + 0; //门票id，根据门票id获取航线及票类
	$select_uuid = "select uuid,pid from uu_jq_ticket where id=$tid limit 1";
	$GLOBALS['le']->query($select_uuid);
	if(!$GLOBALS['le']->fetch_assoc())	exit('101');		
	$uuids = explode("_",$GLOBALS['le']->f('uuid'));  //查询票类id	

	$ls_order = 'pft'.str_replace('.',mt_rand(1000,9999),microtime(true));
	$submit_array = array(
		'ProductCode' 	 	=> $uuids[0],							// 门票ID
		'ProductID' 	 	=> $uuids[1],							// 门票编码
		'ProductPackID' 	=> $uuids[2],							// 产品包ID
		'OrderNO' 			=> $ls_order,							// 临时订单号
		'ProductSDate'      => substr($_REQUEST['PlayDate'],0,10),	// 日期
		'ProductCount'		=> $_REQUEST['Tnum'],					// 数量
		'LinkName'     		=> $_REQUEST['TouristName'],			// 姓名
		'LinkPhone'		 	=> $_REQUEST['TouristTel'],				//手机
		'LinkICNO'			=> $_REQUEST['IdCard'],					// 身份证
	);
	$OrderOccupies  = $QD->OrderOccupies($submit_array);
	if($OrderOccupies == 100){
		if($_REQUEST['PayStatus'] == 1){
			$Ecode = $QD->OrderFinish($ls_order);
			$QD->logfile(microtime(true) - $timestart,'qd_subO_time');
			if($Ecode == 101) exit("101|0|0|0|0|0");
			else exit("200|0|0|0|".$Ecode."|".$ls_order);//青岛没有订单号，将临时订单号作为UUtordernum值记录
		}
	}else{
		exit("101|0|0|0|0|0");
	}
}elseif($_REQUEST['Action'] == 'Relation'){ //下单成功要发短信

	$Ordern = $_REQUEST['Ordern'];
	$pft_order_xml = $PFT_infunction->Select_PFTorder($_REQUEST['Ordern']);
	$ordernum = (string)$pft_order_xml->Rec->UUordernum;
	$lid = (string)$pft_order_xml->Rec->UUlid;
	$tid = (string)$pft_order_xml->Rec->UUtid;
	$aid = (string)$pft_order_xml->Rec->UUaid;
	$fid = (string)$pft_order_xml->Rec->UUbuyid;
	$pid = (string)$pft_order_xml->Rec->UUpid;
	$title = (string)$pft_order_xml->Rec->UUltitle.(string)$pft_order_xml->Rec->UUttitle;
	$tnum = (int)$pft_order_xml->Rec->UUtnum;
	$begintime = (string)$pft_order_xml->Rec->UUbegintime;
	$vcode = (string)$pft_order_xml->Rec->UUvcode;
	$tordernum = (string)$pft_order_xml->Rec->UUtordernum;
	$tel = (string)$pft_order_xml->Rec->UUordertel;

	$str="insert into all_api_order (pftOrder,apiOrder,tempOrder,coopB,oStatus,oStnum,apiCode,handleStatus,cTime) values('$ordernum','$vcode','$tordernum',4,0,$tnum,'$vcode',0,now())"; //青岛没有订单号，将apiOrder作为vcode值记录
	$GLOBALS['le']->query($str);
	
	$link = false;
	if($lid == 9102) $link = true;
	$sms = $PFT_infunction->HQ_send_sms($tel,$aid,$fid,$tid,$pid,$ordernum,$lid,$title,$tnum,$begintime,$vcode,$link);
	if($sms == 100)
		$QD->logfile($ordernum.'|'.$vcode,'send_sms_success');	
}
elseif($_REQUEST['Action'] == 'Relation_after_pay'){  //散客支付订单
	$pft_order_xml = $PFT_infunction->Select_PFTorder($_REQUEST['Ordern']);		
	$UUtordernum = (string)$pft_order_xml->Rec->UUtordernum;	
	$QD->OrderFinish($UUtordernum);
}
elseif($_REQUEST['Action'] == 'MOD'){//取消订单
	if($_REQUEST['Tnum'] != 0) exit("101|0|0|0|0|0");  //没有修改的接口,需要整笔退单
	$pft_order_xml = $PFT_infunction->Select_PFTorder($_REQUEST['Ordern']);		
	$UUtordernum = (string)$pft_order_xml->Rec->UUtordernum;
	$UUvcode = (string)$pft_order_xml->Rec->UUvcode;
	$UUsalerid  = (string)$pft_order_xml->Rec->UUsalerid;
	$UUlid  = (string)$pft_order_xml->Rec->UUlid;
	$UUcode  = (string)$pft_order_xml->Rec->UUcode;
	$chkIns = 499;
	if((int)$pft_order_xml->Rec->UUstatus==2) $chkIns = 498;
	
	// echo $UUvcode;exit;
	$OrderStatus = $QD->GetAllOrderStatus($UUvcode);
	$Status = $OrderStatus[0]['OrderStatus'];
	// echo $Status;exit;
	if($Status == 'S'){	//已付款未检票
		$orderCancel = $QD->ChangeOrderEdit($UUvcode,$UUtordernum);
		if($orderCancel == 100) {
			$str="update all_api_order set oStatus=3,handleStatus=0 where pftOrder='".$_REQUEST['Ordern']."' limit 1";
			$GLOBALS['le']->query($str);
			exit("200|0|0|0|0|0");
		}
		else exit("1077|0|0|0|0|0");
	}elseif($Status == 'O'){ //线下已换票，需更新票付通状态
		$r = $PFT_infunction->Unified_Check_IN($UUsalerid,$UUcode,date('Y-m-d H:i:s'),$chkIns);
		// print_r($r);exit;
		if($r == 100){
			$selorder = "update all_api_order  set oStatus=1 where pftOrder='".$_REQUEST['Ordern']."' limit 1";
			$GLOBALS['le']->query($selorder);
			$QD->logfile($_REQUEST['Ordern'].'|'.$UUvcode.'|'.$UUtordernum,'terminalCheck_success');
		}
		else $QD->logfile($_REQUEST['Ordern'].'|'.$UUvcode.'|'.$UUtordernum.'|'.$terminal,'terminalCheck_fail');
		exit("1077|0|0|0|0|0");
	}elseif($Status == 'A'){ //已取消
		exit("200|0|0|0|0|0");
	}
	else exit("1077|0|0|0|0|0"); 
}

// function logtimes($txt){
	// $fp = fopen('/var/www/html/new/d/meituan/logs/qd_subO_time.txt',"a");
	// flock($fp, LOCK_EX) ;
	// fwrite($fp,date('Y-m-d H:i:s').'|'.$txt."\n");
	// flock($fp, LOCK_UN);
	// fclose($fp);
// }
?>